package com.tiancheng.trade.payment.channel;

import com.tiancheng.commom.pay.PayOrderAutoQueryConfig;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.commom.core.exception.BusinessException;
import com.tiancheng.trade.payment.channel.bo.*;
import com.tiancheng.trade.commom.core.enums.PaymentChannelEnum;
import com.tiancheng.trade.payment.model.PaymentFlowDetailSettle;
import com.tiancheng.trade.payment.model.PaymentFlowDetailSettleDetail;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 支付渠道抽象类
 *
 * @author kellen
 * @date 2024/4/15
 */
public interface AbstractPayChannel {

    PaymentChannelEnum getPaymentChannelEnum();

    /**
     * 统一下单
     *
     * @param unifiedOrderBO 支付参数
     * @return
     * @throws BusinessException
     */
    Result<UnifiedOrderResBO> unifiedOrder(UnifiedOrderBO unifiedOrderBO) throws BusinessException;

    /**
     * 下单查询
     *
     * @param getUnifiedOrderBO
     * @return
     * @throws BusinessException
     */
    Result<GetUnifiedOrderResBO> get(GetUnifiedOrderBO getUnifiedOrderBO) throws BusinessException;

    /**
     * 退款
     *
     * @param refundOrderBO
     * @return
     * @throws BusinessException
     */
    Result<RefundOrderResBO> refund(RefundOrderBO refundOrderBO) throws BusinessException;

    /**
     * 退款查询
     *
     * @param getRefundOrderBO
     * @return
     * @throws BusinessException
     */
    Result<GetRefundOrderResBO> getRefund(GetRefundOrderBO getRefundOrderBO) throws BusinessException;

    /**
     * 关闭订单
     *
     * @param closeOrderBO
     * @return
     * @throws BusinessException
     */
    Result close(CloseOrderBO closeOrderBO) throws BusinessException;
    /**
     * 处理支付结果
     *
     * @param payResult
     * @return
     */
    PayResultBO payResult(Map<String, Object> payResult);

    /**
     * 处理退款结果
     *
     * @param refundResult
     * @return
     */
    RefundResultBO refundResult(Map<String, Object> refundResult);


    /**
     * 核销V2
     *
     * @param detailSettles 汇总信息
     * @param detailSettles 明细信息
     * @return
     * @throws BusinessException
     */
    Result settleV2(List<PaymentFlowDetailSettle> detailSettles, List<PaymentFlowDetailSettleDetail> detailSettlesDetails) throws BusinessException;

    /**
     * 自动查询订单状态
     * @return
     */
    boolean autoQueryStatus(Long orgPaymentChannelId);

    PayOrderAutoQueryConfig getOrderAutoQueryConfig(Long orgPaymentChannelId);

    /**
     * 生成结算信息
     *
     * @param unifiedOrderBO
     * @return
     */
    default UnifiedSettleMerchantBO genSettleInfo(UnifiedOrderBO unifiedOrderBO) {
        UnifiedSettleMerchantBO settlReq = new UnifiedSettleMerchantBO();
        // 平台分账金额
        settlReq.setPlatAmt(unifiedOrderBO.getSubSettles().stream().mapToInt(UnifiedOrderSettleBO::getPlatformAmount).sum());
        // 同步分账
        List<UnifiedSettleMerchantBO.UnifiedSettleMerchantItemBO> items = new ArrayList<>();
        for (UnifiedOrderSettleBO subSettle : unifiedOrderBO.getSubSettles()) {
            for (UnifiedOrderSettleBO.UnifiedOrderSettleItemBO settleItem : subSettle.getSettleItems()) {
                UnifiedSettleMerchantBO.UnifiedSettleMerchantItemBO item = new UnifiedSettleMerchantBO.UnifiedSettleMerchantItemBO();
                item.setMerId(settleItem.getMid());
                item.setMerOrderId(settleItem.getMerOrderId());
                item.setSatAmt(settleItem.getTotalAmount());
                items.add(item);
            }
        }
        settlReq.setMerchantItems(items);
        return settlReq;
    }

}

